#! /usr/bin/env python
#coding=utf-8

"""

    2011 Boris Tatarintsev

    Auxiliary functions
    
"""

import hashtree

def fetchInterests(friendsData, MAX = 10):
    
    # extract the given number of interests    
    interests = { }
    users = friendsData.getKeys()
    for user in users:
        interests_list = friendsData.getNodeData(user, 'I')
        if interests_list != None:
            for interest in interests_list:            
                if interests.has_key(interest):
                    interests[interest] += 1
                else:
                    interests[interest] = 1
        # we've collected user's interests so we can easily
        # remove it from the tree to save some memory
        friendsData.removeNode(user)       
    
    # convert hash to list for sorting    
    output_list = [(x, interests[x]) for x in interests.keys()]
    output_list = sorted(output_list, key = lambda item: item[1], reverse = True)
    
    output_list = output_list[:MAX]
    
    # count how many distinct weights we've got
    tmp = set([x[1] for x in output_list])
    
    aa = len(tmp)
    
    # return the list itself and the number of distinct weights
    return output_list, len(tmp)

def AABBAABB_Intersect(aabb1, aabb2):
    # checks whether 2 aabbs intersect
    if abs(aabb2.ox - aabb1.ox) > aabb1.half_width + aabb2.half_width: return False
    if abs(aabb2.oy - aabb1.oy) > aabb1.half_height + aabb2.half_height: return False
    return True
